package 蓝桥真题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class 小盆友崇拜圈 {
       static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       final static int N=100010;
       static int h[]=new int[N],e[]=new int[N],ne[]=new int[N],idx,n;
       static boolean st[]=new boolean[N];
       static void add(int a,int b){
    	      e[idx]=b;ne[idx]=h[a];h[a]=idx++;
       }
       public static void main(String[] ss) throws IOException {
		      n=Integer.parseInt(br.readLine());
		      ss=br.readLine().split(" ");
		      Arrays.fill(h, -1);
		      for(int i=1;i<=n;i++){
		    	  int idle=Integer.parseInt(ss[i-1]);
		    	  add(i,idle);
		      }
		      int max=0;
		      for(int i=1;i<=n;i++){
		    	  if(!st[i]){
		    		 int t=i;
		    		 while(!st[t]){
		    			  st[t]=true;
		    			  t=e[h[t]];
		    		 }
		    		 int tt=e[h[t]];
		    		 int res=1;
		    		 while(tt!=t){
		    			 tt=e[h[tt]];
		    			 res++;
		    		 }
		    		 max=Math.max(max, res);
		    	  }
		      }
		      System.out.print(max);
	}
}
